package model;

import common.Enums.MethodType;
import common.Enums.ReliabilityType;
import common.Utils.LinkedSet;

import java.util.Date;
import java.util.UUID;

public class Method implements Comparable<Method>
{
	private UUID ID;
	private String name;
	private ReliabilityType reliability;
	private MethodType type;
	private Date createdDate;
	private Date lastUpdated;
	
	private LinkedSet<Criterion> parameters;
	
	public Method()
	{
		initProperties(null);
	}
	
	public Method(UUID ID, MethodType type)
	{
		initProperties(ID);
		this.type = type;
	}
	
	public Method(String name, MethodType type)
	{
		initProperties(null);
		setName(name);
		this.type = type;
	}
	
	public LinkedSet<Criterion> getParameters() 
	{
		if(parameters == null)
			parameters = new LinkedSet<Criterion>();
		
		return parameters;
	}

	public void setParameters(LinkedSet<Criterion> parameters) {
		this.parameters = parameters;
	}
	
	public Method(String name, UUID ID)
	{
		this.ID = ID;
		setName(name);
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public ReliabilityType getReliability() {
		return reliability;
	}
	
	public void setReliability(ReliabilityType reliability) {
		this.reliability = reliability;
	}
	
	public void addParamater(Criterion parameter)
	{
		if(parameter != null)
			parameters.add(parameter);
	}
	
	/**
	 * Returns the ID for the current method
	 * @return Method's ID
	 */
	public UUID getID() {
		return ID;
	}
	
	public MethodType getType()
	{
		return type;
	}
	
	public void setType(MethodType metType)
	{
		type = metType;
	}
	
	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public Date getLastUpdated() {
		return lastUpdated;
	}

	public void setLastUpdated(Date lastUpdated) {
		this.lastUpdated = lastUpdated;
	}
	
	private void initProperties(UUID itemID)
	{
		if(itemID == null)
			itemID = UUID.randomUUID();
		
		ID = itemID;
		type = null;
		reliability = ReliabilityType.GUESS;
		parameters = new LinkedSet<Criterion>();
		createdDate = new Date();
		lastUpdated = new Date();
	}
	
	@Override
	public boolean equals(Object obj) 
	{
		boolean equals = false;
		
		if(obj != null)
		{
			if(obj.getClass() == this.getClass())
				equals = (ID.equals(((Method)obj).getID()));
		}
		
		return equals;
	}
	
	@Override
	public int hashCode() 
	{
		return ID.hashCode();
	}
	
	@Override
	public int compareTo(Method other) 
	{
		return name.compareToIgnoreCase(other.getName());
	}
}
